﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Serialization;
using System.Collections.ObjectModel;
using System.ComponentModel;

namespace SlaktDataReader
{
    [Serializable]
    public class MoveInBook : BookBase<MoveInBookRecord>, SlaktDataReader.IBooks
    {
        public override ObservableCollection<MoveInBookRecord> Items { get; set; }
        public override BookTypes BookType { get { return BookTypes.MoveIn; } }

        public MoveInBook(string fileName)
        {
            this.FileName = FileName;
            _init();
            ReadBook();
        }

        public MoveInBook() { _init(); }

        protected override void bwLoad_DoWork(object sender, DoWorkEventArgs e)
        {
            MoveInBook rv;
            BWArgs arg = e.Argument as BWArgs;

            switch (arg.FileType)
            {
                case BWArgs.BookFileType.Text:
                    rv = _ReadTextFile(arg.FileName);
                    e.Result = rv;
                    break;
                case BWArgs.BookFileType.Excel:
                    rv = _ReadExcelFile(arg.FileName);
                    e.Result = rv;
                    break;
                default:
                    break;
            }


        }

        protected override void bwLoad_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            if (e.Result != null)
            {
                MoveInBook rv = e.Result as MoveInBook;
                if (rv.Items != null)
                {
                    //this.Area = rv.Area;
                    //this.EditedBy = rv.EditedBy;

                    //this.Parish = rv.Parish;
                    //this.Period = rv.SDCode;
                    //this.Source = rv.Source;
                    //this.UpdateDate = rv.UpdateDate;

                    this.Items = rv.Items;

                    readSuccess();

                    return;
                }
            }
            readFail();
        }

        protected override void bwLoad_ProgressChanged(object sender, ProgressChangedEventArgs e)
        {
        }

        private MoveInBook _ReadTextFile(string fName)
        {
            MoveInBookTextFile reader = new MoveInBookTextFile();
            MoveInBook bBook = new MoveInBook();
            if (reader.Read(fName) > 0)
            {
                bBook.Items = new ObservableCollection<MoveInBookRecord>(reader.Records);
                bBook.FileName = fName;
                // Copy other data
                return bBook;
            }
            return null;
        }

        private MoveInBook _ReadExcelFile(string fName)
        {
            return null;
        }

        public override void ExportBook()
        {
            exportFail();
        }
    }

    [Serializable]
    public class MoveInBookRecord
    {
    }
}
